package com.calendar.api.controller;

import com.calendar.api.model.ApiResponse;
import com.calendar.api.model.Task;
import com.calendar.api.model.dto.TaskDTO;
import com.calendar.api.security.JwtTokenProvider;
import com.calendar.api.service.TaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping("/tasks")
@Api(tags = "任务管理")
public class TaskController {

    private final TaskService taskService;
    private final JwtTokenProvider jwtTokenProvider;

    @Autowired
    public TaskController(TaskService taskService, JwtTokenProvider jwtTokenProvider) {
        this.taskService = taskService;
        this.jwtTokenProvider = jwtTokenProvider;
    }

    @GetMapping
    @ApiOperation("获取任务列表")
    public ApiResponse<List<Task>> getTasks(@RequestParam(required = false) String categoryId, HttpServletRequest request) {
        String token = jwtTokenProvider.resolveToken(request);
        Long userId = jwtTokenProvider.getUserId(token);
        return ApiResponse.success(taskService.getTasks(categoryId, userId));
    }

    @GetMapping("/date/{date}")
    @ApiOperation("获取指定日期的任务")
    public ApiResponse<List<Task>> getTasksByDate(
            @PathVariable @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date,
            HttpServletRequest request) {
        String token = jwtTokenProvider.resolveToken(request);
        Long userId = jwtTokenProvider.getUserId(token);
        return ApiResponse.success(taskService.getTasksByDueDate(date, userId));
    }

    @GetMapping("/{id}")
    @ApiOperation("获取单个任务")
    public ApiResponse<Task> getTask(@PathVariable Long id, HttpServletRequest request) {
        String token = jwtTokenProvider.resolveToken(request);
        Long userId = jwtTokenProvider.getUserId(token);
        return ApiResponse.success(taskService.getTask(id, userId));
    }

    @PostMapping
    @ApiOperation("创建任务")
    public ApiResponse<Task> createTask(@Valid @RequestBody TaskDTO taskDTO, HttpServletRequest request) {
        String token = jwtTokenProvider.resolveToken(request);
        Long userId = jwtTokenProvider.getUserId(token);
        return ApiResponse.success(taskService.createTask(taskDTO, userId));
    }

    @PutMapping("/{id}")
    @ApiOperation("更新任务")
    public ApiResponse<Task> updateTask(@PathVariable Long id, @Valid @RequestBody TaskDTO taskDTO, HttpServletRequest request) {
        String token = jwtTokenProvider.resolveToken(request);
        Long userId = jwtTokenProvider.getUserId(token);
        return ApiResponse.success(taskService.updateTask(id, taskDTO, userId));
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除任务")
    public ApiResponse<Void> deleteTask(@PathVariable Long id, HttpServletRequest request) {
        String token = jwtTokenProvider.resolveToken(request);
        Long userId = jwtTokenProvider.getUserId(token);
        taskService.deleteTask(id, userId);
        return ApiResponse.success();
    }

    @PatchMapping("/{id}/toggle")
    @ApiOperation("切换任务完成状态")
    public ApiResponse<Task> toggleTaskStatus(@PathVariable Long id, HttpServletRequest request) {
        String token = jwtTokenProvider.resolveToken(request);
        Long userId = jwtTokenProvider.getUserId(token);
        return ApiResponse.success(taskService.toggleTaskStatus(id, userId));
    }
} 